package com.spdg.ring;

import java.util.ArrayList;
import java.util.List;

import android.content.Intent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;
import cn.wolf.http.HttpCallBack;
import cn.wolf.tools.StringUtil;

import com.spdg.ring.adapter.GiftListAdapter;
import com.spdg.ring.adapter.GridAdapter;
import com.spdg.ring.adapter.ListAdapter;
import com.spdg.ring.bo.ContentBo;
import com.spdg.ring.common.Key;
import com.spdg.ring.datamgr.ArticleDataMgr;
import com.spdg.ring.entity.ContentEntity;
import com.spdg.ring.resp.ContentResp;
import com.spdg.ring.sqlite.model.ArticleList;
import com.spdg.ring.sqlite.model.Menu;
import com.spdg.ring.widget.PullToRefreshBase.OnRefreshListener;
import com.spdg.ring.widget.PullToRefreshGridView;
import com.spdg.ring.widget.PullToRefreshListView;

/**
 * <p>Title:  玩乐速递</p>
 * <p>Description:  </p>
 * <p>@author: Eric.wsd                </p>
 * <p>Copyright: Copyright (c) 2012    </p>
 * <p>Company: HX Co., Ltd.          </p>
 * <p>Create Time: 2012-10-18             </p>
 * <p>Update Time:                     </p>
 * <p>Updater:                         </p>
 * <p>Update Comments:                 </p>
 */
public class HomeActivity extends RingBaseActivity {

	public static final String TYPE_CONTENT_ITEM = "0"; // 子项内容列表
	public static final String TYPE_MENU_ITEM = "1"; // 菜单内容列表

	private TextView mHomeTitle;
	private PullToRefreshListView mPullToRefreshListView;
	private PullToRefreshGridView mPullToRefreshGridView;
	private ListView mHomeListView;
	private GridView mHomeGridView;
	private ListAdapter mListAdapter;
	private GridAdapter mGridAdapter;
	private GiftListAdapter mGiftListAdapter;
	private TextView mTopTitle;
	private RelativeLayout mTop;

	// data init
	private String mLink; // 请求链接
	private String mType; // 布局类型
	private String mTitle; // top标题
	private ContentBo bo = null;
	private List<ArticleList> mContentItemList = new ArrayList<ArticleList>();
	private Menu menu; // 菜单数据
	private ArticleList mArticleList; // 子项内容数据
	private String fileKey; // parent key

	@Override
	protected void initComponents() {
		mHomeTitle = (TextView) findViewById(R.id.home_title);
		mPullToRefreshListView = (PullToRefreshListView) findViewById(R.id.home_listview);
		mPullToRefreshGridView = (PullToRefreshGridView) findViewById(R.id.home_gridview);
		mHomeListView = mPullToRefreshListView.getRefreshableView();
		mHomeGridView = mPullToRefreshGridView.getRefreshableView();
		mTopTitle = (TextView) findViewById(R.id.top_title);
		mTop = (RelativeLayout) findViewById(R.id.top);

		// 设置监听器
		mHomeListView.setOnItemClickListener(new ContentOnItemClickListener());
		mHomeGridView.setOnItemClickListener(new ContentOnItemClickListener());
		mPullToRefreshListView.setOnRefreshListener(new PullDown());
		mPullToRefreshGridView.setOnRefreshListener(new PullDown());

	}

	@Override
	protected void initData() {
		Intent i = getIntent();

		String itemType = i.getStringExtra(Key.K_ITEM_TYPE);
		if (TYPE_CONTENT_ITEM.equals(itemType)) {
			mArticleList = (ArticleList) i.getSerializableExtra(Key.K_CONTENT_INFO);
			if (mArticleList == null)
				return;

			mLink = mArticleList.getSrc();
			mType = mArticleList.getType();
			fileKey = mArticleList.getFile();
		} else {
			menu = (Menu) i.getSerializableExtra(Key.K_MENU_INFO);

			if (menu == null)
				return;

			mLink = menu.getSrc();
			mType = menu.getType();
			fileKey = menu.getFile();
		}

		mTitle = i.getStringExtra(Key.K_TOP_TITLE); // Top标题

		// 初始化Top
		if (StringUtil.isEmpty(mTitle)) {
			mTop.setVisibility(View.GONE);
		} else {
			mTopTitle.setText(mTitle);
			mTop.setVisibility(View.VISIBLE);
		}

		if (!StringUtil.isEmpty(fileKey)) {
			// 加载数据库缓存
			ArticleDataMgr articleMgr = ArticleDataMgr.getInstance();
			List<ArticleList> articles = articleMgr.getArticleList(mContext, fileKey);

			initListView("", articles);

			// 请求数据
			bo = new ContentBo(this, mLink, fileKey, new ContentCallback());
			bo.request();
		}

	}

	/**
	 * 初始化适配器
	 * @param type
	 * 		包括：1. item；2. table；3.ImageList
	 */
	private void initAdapter() {
		if (Key.K_CONTENT_TYPE_TABLE.equals(mType)) {
			if (mListAdapter != null) {
				mListAdapter.notifyDataSetChanged();
			} else {
				mListAdapter = new ListAdapter(mContext, mContentItemList);
				mHomeListView.setAdapter(mListAdapter);
			}

			// 修改显示界面
			mPullToRefreshListView.setVisibility(View.VISIBLE);
			mPullToRefreshGridView.setVisibility(View.GONE);
		} else if (Key.K_CONTENT_TYPE_IMAGELIST.equals(mType)) {
			if (mGridAdapter != null) {
				mGridAdapter.notifyDataSetChanged();
			} else {
				mGridAdapter = new GridAdapter(mContext, mContentItemList);
				mHomeGridView.setAdapter(mGridAdapter);
			}

			// 修改显示界面
			mPullToRefreshListView.setVisibility(View.GONE);
			mPullToRefreshGridView.setVisibility(View.VISIBLE);
		} else if (Key.K_CONTENT_TYPE_LISTVIEW.equals(mType)) {
			if (mGiftListAdapter != null) {
				mGiftListAdapter.notifyDataSetChanged();
			} else {
				mGiftListAdapter = new GiftListAdapter(mContext, mContentItemList);
				mHomeListView.setAdapter(mGiftListAdapter);
			}

			// 修改显示界面
			mPullToRefreshListView.setVisibility(View.VISIBLE);
			mPullToRefreshGridView.setVisibility(View.GONE);
		}
	}

	public void initListView(String slogan, List<ArticleList> articles) {

		if (articles != null) {
			mContentItemList.clear();
			mContentItemList.addAll(articles);

			initAdapter();
			if (StringUtil.isEmpty(slogan)) {
				mHomeTitle.setVisibility(View.GONE);
			} else {
				mHomeTitle.setText(slogan);
				mHomeTitle.setVisibility(View.VISIBLE);
			}

			hideRefreshView(); // 隐藏下拉刷新界面
			hideProgressBar();
		}
	}

	/**
	 * 隐藏下拉刷新界面
	 */
	public void hideRefreshView() {
		mPullToRefreshListView.onRefreshComplete();
		mPullToRefreshGridView.onRefreshComplete();
	}

	class ContentOnItemClickListener implements OnItemClickListener {

		@Override
		public void onItemClick(AdapterView<?> parent, View v, int pos, long id) {
			BaseAdapter adapter = (BaseAdapter) parent.getAdapter();

			ArticleList entity = (ArticleList) adapter.getItem(pos);
			if (entity != null) {
				mLink = entity.getSrc();
				mType = entity.getType();
				mTitle = entity.getTitle();

				if (!Key.K_CONTENT_TYPE_ITEM.equals(mType)) {
					Intent i = new Intent(HomeActivity.this, HomeActivity.class);
					i.putExtra(Key.K_CONTENT_INFO, entity);
					i.putExtra(Key.K_TOP_TITLE, mTitle);
					i.putExtra(Key.K_ITEM_TYPE, TYPE_CONTENT_ITEM);
					i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
					startActivity(i);
				} else {
					// 若类型为Item，则跳转到详细页面
					// FIXME 需要传参数给详细页面
					Intent i = new Intent(HomeActivity.this, ArticleActivity.class);
					i.putExtra(Key.K_ARTICLE_INFO, entity);
					i.putExtra(Key.K_FILE_KEY, fileKey);
					i.putExtra(Key.K_CONTENT_POS, pos);
					startActivity(i);
				}
			}
		}
	}

	class ContentCallback implements HttpCallBack<ContentResp> {

		@Override
		public void call(ContentResp response) {
			ContentEntity entity = response.getmContentEntity();
			if (entity != null) {
				initListView(entity.getSlogan(), entity.getItemList());
			}
		}

		@Override
		public void progress(Object... obj) {
		}

		@Override
		public void onNetWorkError() {
			hideRefreshView();
			hideProgressBar();
			Toast.makeText(mContext, getString(R.string.network_error), Toast.LENGTH_SHORT).show();
		}
	}

	class PullDown implements OnRefreshListener {
		@Override
		public void onRefresh() {
			if (bo != null)
				bo.request();
		}
	}

	@Override
	protected int getMainContentViewId() {
		return R.layout.act_home;
	}

	@Override
	protected Class<?> getResouceClass() {
		return R.class;
	}

	@Override
	protected String getEventID() {
		return getString(R.string.event_activity_home);
	}
}
